#include "view_music.h"
#include "display_ctrl.h"
#include "platform_assert.h"
#include "lfs_file_manager_auto.h"
#include "rtc.h"
#include "datetime.h"
#include "blecom.h"
#include "battery_ctrl.h"
#include "sport_his.h"
#include "display_ctrl.h"
#include "display_interface_240x240.h"
#include "datetime.h"
#include "heart_rate_interface.h"
#include "font/num_font.h"
#include "view_common.h"
#include "arm_math.h"
#include "lfs_file_manager.h"
#include "app_trace.h"
#include "app_timer.h"
#include "Motor.h"
#include "language_text.h"
// #include "view_music.inc"
#include "stopwatch.h"
#include "localize_string.h"
void view_music_base_display(void)
{
#ifdef CUSTOM_UI
    int const temp_index_start = 50;
    int custom_ui_index = 0;
    for (int i = 0; i < MUSIC_BACK_COUNT; i++)
    {
        disp_show_resource_with_x_y(DISP_LFS_FILE_INDEX_0 + custom_ui_index, localize_resource_key_music_back_start + i, temp_index_start + custom_ui_index, -1, -1);
        ++custom_ui_index;
    }
#if MUSIC_GIF_COUNT > 0
    disp_show_resource_with_x_y(DISP_LFS_FILE_INDEX_0 + custom_ui_index, localize_resource_key_music_gif_start + disp_ctrl.auto_refresh_count % HEART_RATE_GIF_COUNT,
                                temp_index_start + custom_ui_index, -1, -1);
    ++custom_ui_index;
#endif
    int x = 87;
    int y = 182;
    x = 46;
    y = 177;
    if (disp_ctrl.com_data.uint8_temp[100])
    {
        y -= 3;
        disp_show_font_string_utf8_mid(y, (char const *)(&disp_ctrl.com_data.uint8_temp[101]), 0);
    }
    else
    {
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0 + custom_ui_index, x, y, FILE_IMG_K105_music_text_46_177);
    }

    ++custom_ui_index;
#else
    int x = 67;
    int y = 41;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_K104_music_icon_67_41);
    x = 46;
    y = 177;
    if (disp_ctrl.com_data.uint8_temp[100])
    {
        y -= 3;
        disp_show_font_string_utf8_mid(y, (char const *)(&disp_ctrl.com_data.uint8_temp[101]), 0);
    }
    else
    {
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, x, y, FILE_IMG_K105_music_text_46_177);
    }
    show_button_up();
    show_button_right();
    show_button_down();
#endif
}

void view_music_state_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        if (!ble_if_connected())
        {
            display_show(view_music, DISP_MODE_NORMAL);
            return;
        }
    }
    int x = 0;
    int y = 0;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_K101_music_back);

    x = 77;
    y = 189;
    if (disp_ctrl.com_data.uint8_temp[100])
    {
        y -= 4;
        disp_show_font_string_utf8_mid(y, (char const *)(&disp_ctrl.com_data.uint8_temp[101]), 0);
    }
    else
    {
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, x, y, FILE_IMG_k101_music_control_77_189);
    }
}
int32_t view_music_display(int32_t view)
{
    switch (view)
    {
    case view_music_base:
        view_music_base_display();
        break;
    case view_music_state:
        view_music_state_display();
        break;
    }
    return 0;
}
view_event_result_t view_music_base_event(event_param_t param)
{
    switch (param.event)
    {
    case event_view_entry:
    {
        uint8_t *buff = &disp_ctrl.com_data.uint8_temp[101];
        uint32_t buff_len = disp_ctrl.com_data.uint8_temp + sizeof(disp_ctrl.com_data.uint8_temp) - buff;
        disp_ctrl.com_data.uint8_temp[100] = get_language_text(language_text_index_Music_control, buff, buff_len);
        return view_event_result_handled;
    }
    case event_move_level_down:
        if (ble_if_connected())
        {
            display_show(view_music_state, DISP_MODE_NORMAL);
        }
        else
        {
            display_show(view_prompt_music_not_connect, DISP_MODE_NORMAL);
        }
        return view_event_result_handled;
    }
    return view_event_result_not_handled;
}
view_event_result_t view_music_state_event(event_param_t param)
{
    switch (param.event)
    {
    case event_view_entry:
    {
        uint8_t *buff = &disp_ctrl.com_data.uint8_temp[101];
        uint32_t buff_len = disp_ctrl.com_data.uint8_temp + sizeof(disp_ctrl.com_data.uint8_temp) - buff;
        disp_ctrl.com_data.uint8_temp[100] = get_language_text(language_text_index_Music_control, buff, buff_len);
        return view_event_result_handled;
    }
    case event_ble_disconnect:
        display_show(view_music, DISP_MODE_NORMAL);
        return view_event_result_handled;
    case event_move_next_view:
        return view_event_result_handled;
    case event_move_level_down:
        return view_event_result_handled;
    case BUTTON_EVENT_TP_TAP:
        if (param.x < 80)
        {
            music_ctrl_action(music_ctrl_action_previous);
        }
        else if (param.x < 160)
        {
            music_ctrl_action(music_ctrl_action_start_stop);
        }
        else
        {
            music_ctrl_action(music_ctrl_action_next);
        }
        return view_event_result_handled;
    case BUTTON_EVENT_SHORT_PRESS:
    {
        switch (param.source)
        {
#if defined(BUTTON_TOP)
        case event_source_button_top:
            music_ctrl_action(music_ctrl_action_previous);
            break;
#endif
#if defined(BUTTON_MIDDLE)
        case event_source_button_middle:
            music_ctrl_action(music_ctrl_action_start_stop);
            break;
#endif
#if defined(BUTTON_BOTTOM)
        case event_source_button_bottom:
            music_ctrl_action(music_ctrl_action_next);
            break;
#endif
        }
    }
        return view_event_result_handled;
    }
    return view_event_result_not_handled;
}
view_event_result_t view_music_event(int32_t view, event_param_t param)
{
    switch (view)
    {
    case view_music_base:
        return view_music_base_event(param);
    case view_music_state:
        return view_music_state_event(param);
    }
    return view_event_result_not_handled;
}
